package org.joinme.backend.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author MingQi
 *
 */
public class DateUtil {
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	
	public static String getCurrentTime() {
		return DateUtil.format(new Date(), DATE_TIME_FORMAT);
	}
	
	public static String getCurrentDate() {
		return DateUtil.format(new Date(), DATE_FORMAT);
	}
	
	public static String valueOf(long currentTimeMillis) {
		return DateUtil.format(new Date(currentTimeMillis), DATE_TIME_FORMAT);
	}

	public static String valueOf(int currentTimeSeconds) {
		return DateUtil.format(new Date(currentTimeSeconds * 1000L), DATE_TIME_FORMAT);
	}
	
	public static String timeAfterYears(String current, int yearCount){
		String[] time = current.split("-");
		int year = Integer.parseInt(time[0]) + yearCount;
		return year + "-" + time[1] + "-" + time[2];
	}
	
	public static String timeAfterYears(int yearCount){
		String current = getCurrentTime();
		String[] time = current.split("-");
		int year = Integer.parseInt(time[0]) + yearCount;
		return year + "-" + time[1] + "-" + time[2];
	}
	
	public static String timeAfterDays(String current, int day){
		return timeAfterHours(current, day * 24);
	}
	
	public static long timeAfterDaysMillis(String current, int day){
		return timeAfterHoursMillis(current, day * 24);
	}

	public static String dateAfterDays(int day) {
		return DateUtil.format(new Date(System.currentTimeMillis()+day*86400000), DATE_FORMAT);
	}
	
	/**
	 * 解析时间字符串
	 * @param time timestamp in format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code>.
	 * @return
	 */
	public static long valueOf(String time){
		Timestamp cur = Timestamp.valueOf(time);
		return cur.getTime();
	}
	
	public static String timeAfterHours(String current, int hourCount){
		Timestamp cur = Timestamp.valueOf(current);
		Date date = new Date(cur.getTime()+hourCount*3600000);
		return DateUtil.format(date, DATE_TIME_FORMAT);
	}
	
	public static long timeAfterHoursMillis(String current, int hourCount){
		Timestamp cur = Timestamp.valueOf(current);
		return cur.getTime()+hourCount*3600000;
	}
	
	public static String format(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
}
